package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import server.database.ConnectionDatabase;

import mockup.Mockup;
import mockup.ReservationMockup;

//not tested yet

public class ReservationDB implements DatabaseAction {

	private String query;
	private Statement st;
	
	public ReservationDB() {
		try {
			st = ConnectionDatabase.getConnection().createStatement();			
		} catch (Exception e) {
			System.err.println("AppoitmentDB::Constructor:Exception: " + e.getMessage());
		}
	}

	@Override
	public void delete(Mockup m) {
		ReservationMockup res = (ReservationMockup) m;
		if(res.getId() > -1) {
			query = "DELETE FROM " + ConnectionDatabase.getNameDB() + ".reservation " +
			"where reservation_id = " + res.getId();

			ConnectionDatabase.update(query, st);
		}
	}

	@Override
	public ReservationMockup get(int id) {
		if(id > -1) {
			query = "select * from " + ConnectionDatabase.getNameDB() + ".reservation " +
			"where reservation_id = " + id;
			ResultSet rs = ConnectionDatabase.get(query, st);
			try {
				while (rs.next()) {
					int res_id = rs.getInt("reservation_id");
					long start = rs.getLong("starttime");
					long end = rs.getLong("endtime");
					int room_id = rs.getInt("room_id");

					ReservationMockup res = new ReservationMockup(res_id, start, end, room_id);
					return res;
				}

			} catch (SQLException e ) {
				System.err.println("ReservationDB::get:SQLException: " + e.getMessage());
			}
		} else {
			System.err.println("0 > id , søker ikke i database");
		}
		return null;
	}

	@Override
	public int insert(Mockup m) {
		int dbID = -1;
		ReservationMockup res = (ReservationMockup) m;
		if(0 > res.getId() ) {

			Object room_id;
			if(res.getRoomID() == -1 ) {
				room_id = null;
			} else {
				room_id = res.getRoomID();
			}

			query = "INSERT INTO "+ ConnectionDatabase.getNameDB()+".reservation (" +
			"starttime, endtime, room_id) " +		
			"values(" + res.getStartTime()  + "," +
			res.getEndTime() + "," +
			room_id + ")";

			dbID = ConnectionDatabase.insert(query);

		} else {
			update(m);
		}

		return dbID;
	}

	@Override
	public void update(Mockup m) {
		ReservationMockup res = (ReservationMockup) m;
		if(res.getId() > 0) {

			Object room_id;
			if(res.getRoomID() <= 0 ) {
				room_id = null;
			} else {
				room_id = res.getRoomID();
			}

			query = "UPDATE " + ConnectionDatabase.getNameDB() + ".reservation SET " +
			"starttime = " + res.getStartTime() + "," +
			"endtime = " + res.getEndTime() + "," + 
			"room_id = " + room_id + " " +
			"where reservation_id = " + res.getId();

			ConnectionDatabase.update(query, st);

		} else {
			System.err.println("Reservasjon har ikke ID; bruk insert i stedet");
		}
	}

	@Override
	public List<ReservationMockup> getAll() {
		List<ReservationMockup> reservations = new ArrayList<ReservationMockup>();
		query = "select * from " + ConnectionDatabase.getNameDB() + ".reservation";
		ResultSet rs = ConnectionDatabase.get(query, st);
		try {
			while (rs.next()) {
				int id = rs.getInt("reservation_id");
				long start = rs.getLong("starttime");
				long end = rs.getLong("endtime");
				int room_id = rs.getInt("room_id");

				ReservationMockup res = new ReservationMockup(id, start, end, room_id);
				reservations.add(res);
			}

		} catch (SQLException e ) {
			System.err.println("ReservationDB::getAll:SQLException: " + e.getMessage());
		}
		return reservations;
	}
	
	@Override
	public void emptyTable() {
		query = "TRUNCATE TABLE " + ConnectionDatabase.getNameDB() + ".reservation";
		ConnectionDatabase.update(query, st);
	}
}
